package hotel.action;

import hotel.form.RoomForm;
import hotel.service.RoomService;
import hotel.service.ServiceManager;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
/**
 * This class create new room
 * @author Alex
 *
 */
public class RoomAction extends Action 
{
	private static final Logger logger = Logger.getLogger(RoomAction.class);
	private static final String ROOMACTION_ERROR = "Error in RoomAction:";
	
	@Override
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws Exception 
    {
		String mapper = ForwardActionTo.ERROR;
		try
		{
		    RoomForm roomForm = (RoomForm)form;
		    ActionMessages messages = new ActionMessages();
		    
			if (roomForm != null) 
		     {
		    	RoomService roomService = ServiceManager.getDefault().getRoomService();
		        roomService.insert(roomForm.getRoomClass(), roomForm.getBeds());
		        mapper = ForwardActionTo.SUCCESS;
		        messages.add("roomadd_success", new ActionMessage("message.room.add.success"));
		        saveMessages(request, messages);
		     }
		}
		catch(NullPointerException e)
		{
			logger.error(ROOMACTION_ERROR + e);
		}
		
		return mapping.findForward(mapper);
    }


}
